package br.com.lazyprogrammer.worldsoccermanager.fragments.create;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.Calendar;

import br.com.lazyprogrammer.worldsoccermanager.R;
import br.com.lazyprogrammer.worldsoccermanager.fragments.interfaces.OnNextListener;
import br.com.lazyprogrammer.worldsoccermanager.network.model.Team;
import br.com.lazyprogrammer.worldsoccermanager.system.generator.Generator;
import br.com.lazyprogrammer.worldsoccermanager.system.message.Message;
import br.com.lazyprogrammer.worldsoccermanager.util.PoolData;

/**
 * Created by Andre on 08/09/2014.
 */
public class SelectTeamNameFragment extends Fragment implements View.OnClickListener, Spinner.OnItemSelectedListener {

    private OnNextListener listener;

    private Button buttonNext;
    private EditText inputFullname, inputShortname;
    private Spinner spinnerBackground, spinnerForeground;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        listener = (OnNextListener) activity;
    }

    @Override
    public void onClick(View view) {
        if (view.equals(buttonNext))
            next();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_create_team_step00, container, false);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    @Override
    public void onResume() {
        super.onResume();

        buttonNext = (Button) getView().findViewById(R.id.button_next);

        inputFullname = (EditText) getView().findViewById(R.id.input_team_fullname);
        inputShortname = (EditText) getView().findViewById(R.id.input_team_shortname);

        spinnerBackground = (Spinner) getView().findViewById(R.id.spinner_background);
        spinnerForeground = (Spinner) getView().findViewById(R.id.spinner_foreground);

        buttonNext.setOnClickListener(this);

        spinnerBackground.setOnItemSelectedListener(this);
        spinnerForeground.setOnItemSelectedListener(this);
    }

    private String getTeamFullnameInputText() {
        return inputFullname.getText().toString();
    }

    private String getTeamShortnameInputText() {
        return inputShortname.getText().toString();
    }

    private boolean isTeamFullnameInputFilled() {
        return !getTeamFullnameInputText().trim().equals("");
    }

    private boolean isTeamShortnameInputFilled() {
        return !getTeamShortnameInputText().trim().equals("");
    }

    private void next() {
        if (isTeamFullnameInputFilled() && isTeamShortnameInputFilled()) {
            String teamId = Generator.encodeCustomId(getTeamFullnameInputText(), System.currentTimeMillis(), getResources().getConfiguration().locale);
            PoolData.team = new Team(teamId, getTeamShortnameInputText(), getTeamFullnameInputText());
            PoolData.team.setBackground(spinnerBackground.getSelectedItemPosition());
            PoolData.team.setForeground(spinnerForeground.getSelectedItemPosition());
            listener.goToNext(1);
        }
        else
            Message.send(getActivity(), R.string.exception_empty_field);
    }
}
